Most of the below code is copied/inspired from https://www.pythonsheets.com/notes/python-sqlalchemy.html
In [9]:
def create_db():
import os
DB_FILE = "db.sqlite"
if os.path.exists(DB_FILE):
os.remove(DB_FILE)
db_uri = "sqlite:///" + DB_FILE
return create_engine(db_uri)
In [10]:
from sqlalchemy import create_engine
engine = create_db()
# DBAPI - PEP249
# create table
engine.execute('CREATE TABLE "EX1" ('
'id INTEGER NOT NULL,'
'name VARCHAR, '
'PRIMARY KEY (id));')
# insert a raw
engine.execute('INSERT INTO "EX1" '
'(id, name) '
'VALUES (1,"raw1")')
# select *
result = engine.execute('SELECT * FROM '
'"EX1"')
for _r in result:
print(_r)
# delete *
engine.execute('DELETE from "EX1" where id=1;')
result = engine.execute('SELECT * FROM "EX1"')
print (result.fetchall())
In [6]:
from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy import Column
from sqlalchemy import Integer, String
engine = create_db()
# Create a metadata instance
metadata = MetaData(engine)
# Declare a table
table = Table('Example',metadata,
Column('id',Integer, primary_key=True),
Column('name',String))
# Create all tables
metadata.create_all()
for _t in metadata.tables:
print ("Table: ", _t)
In [8]:
from sqlalchemy import create_engine
from sqlalchemy import inspect
engine = create_db()
inspector = inspect(engine)
# Get table information
print inspector.get_table_names()
# Get column information
print inspector.get_columns('EX1')
In [10]:
from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import Table
engine = create_db()
# Create a MetaData instance
metadata = MetaData()
print metadata.tables
# reflect db schema to MetaData
metadata.reflect(bind=engine)
print metadata.tables
In [12]:
from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy import Column
from sqlalchemy import Integer, String
engine = create_db()
meta = MetaData(engine)
# Register t1, t2 to metadata
t1 = Table('EX1', meta,
Column('id',Integer, primary_key=True),
Column('name',String))
t2 = Table('EX2', meta,
Column('id',Integer, primary_key=True),
Column('val',Integer))
# Create all tables in meta
meta.create_all()
In [11]:
from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy import Column
from sqlalchemy import Integer, String
engine = create_db()
meta = MetaData(engine)
t1 = Table('Table_1', meta,
Column('id', Integer, primary_key=True),
Column('name',String))
t2 = Table('Table_2', meta,
Column('id', Integer, primary_key=True),
Column('val',Integer))
t1.create()
In [ ]:
In [12]:
from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import inspect
from sqlalchemy import Table
from sqlalchemy import Column, Integer, String
from sqlalchemy.engine.url import URL
engine = create_db()
m = MetaData()
table = Table('Test', m,
Column('id', Integer, primary_key=True),
Column('key', String, nullable=True),
Column('val', String))
table.create(engine)
inspector = inspect(engine)
print ('Test' in inspector.get_table_names())
table.drop(engine)
inspector = inspect(engine)
print ('Test' in inspector.get_table_names())
In [18]:
# Think Column as "ColumnElement"
# Implement via overwrite special function
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy import Column
from sqlalchemy import Integer, String
from sqlalchemy import or_
meta = MetaData()
table = Table('example', meta,
Column('id', Integer, primary_key=True),
Column('l_name', String),
Column('f_name', String))
# sql expression binary object
print repr(table.c.l_name == 'ed')
# exhbit sql expression
print str(table.c.l_name == 'ed')
print repr(table.c.f_name != 'ed')
# comparison operator
print repr(table.c.id > 3)
# or expression
print (table.c.id > 5) | (table.c.id < 2)
# Equal to
print or_(table.c.id > 5, table.c.id < 2)
# compare to None produce IS NULL
print (table.c.l_name == None)
# Equal to
print (table.c.l_name.is_(None))
# + means "addition"
print (table.c.id + 5)
# or means "string concatenation"
print (table.c.l_name + "some name")
# in expression
print (table.c.l_name.in_(['a','b']))
In [21]:
from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import String
engine = create_db()
# create table
meta = MetaData(engine)
table = Table('user', meta,
Column('id', Integer, primary_key=True),
Column('l_name', String),
Column('f_name', String))
meta.create_all()
# insert data via insert() construct
ins = table.insert().values(
l_name='Hello',
f_name='World')
conn = engine.connect()
conn.execute(ins)
# insert multiple data
conn.execute(table.insert(),[
{'l_name':'Hi','f_name':'bob'},
{'l_name':'yo','f_name':'alice'}])
Out[21]:
In [20]:
from sqlalchemy import create_engine
from sqlalchemy import MetaData
# engine = create_db()
conn = engine.connect()
meta = MetaData(engine)
user_t = meta.tables['user']
# select * from user_t
sel_st = user_t.select()
res = conn.execute(sel_st)
for _row in res: print (_row)
# delete l_name == 'Hello'
del_st = user_t.delete().where(
user_t.c.l_name == 'Hello')
print '----- delete -----'
res = conn.execute(del_st)
# check rows has been delete
sel_st = user_t.select()
res = conn.execute(sel_st)
for _row in res: print (_row)